const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
    mode: 'production',
    entry: path.join(__dirname, 'src', 'main.ts'), // 单个入口文件
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'webPackbuild'),
        filename: 'index.js', // 生成的文件名
        libraryTarget: 'commonjs2',
        publicPath: '/',
    },
    resolve: {
        extensions: ['.js', '.ts', '.json'],
        alias: {
            '@': path.resolve(__dirname, 'src'),
            '#': path.resolve(__dirname, '.'),
        },
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.js$/,
                include: /node_modules/, // 包含 node_modules 中的 .js 文件
                use: 'babel-loader',
            },
           
        ],
    },
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin()],
    },
    externals: {
        'class-transformer': 'commonjs class-transformer',
        'class-validator': 'commonjs class-validator',
        'cache-manager': 'commonjs cache-manager',
        '@nestjs/microservices': 'commonjs @nestjs/microservices',
        '@nestjs/websockets': 'commonjs @nestjs/websockets',
        '@nestjs/core': 'commonjs @nestjs/core',
        '@nestjs/common': 'commonjs @nestjs/common',
        // react: 'React',
        // 'react-dom': 'ReactDOM',
        // // 对于模块路径的 externals，可以使用正则表达式
        '^@nestjs/(.*)$': 'commonjs @nestjs/\$1',
    },
    plugins: [
        new CleanWebpackPlugin(),
        // new webpack.IgnorePlugin({
        //     // resourceRegExp: /^(class-transformer|class-validator|cache-manager|@nestjs\/microservices|@nestjs\/websockets)$/,
        // }),
        // new webpack.DefinePlugin({
        //     MODULE_PATH: JSON.stringify('libs/dynamic-modules-loader'),
        // }),
    ],
};
